//
//  QRCodeManager.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/12/11.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "QRCodeManager.h"

@implementation QRCodeManager
+ (UIImage *)getQRCodeWithInfo:(NSString *)qrCodeInfo{
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    
    NSData *data = [qrCodeInfo dataUsingEncoding:NSUTF8StringEncoding];
    
    [filter setValue:data forKey:@"inputMessage"];
    
    CIImage *outputImage = [filter outputImage];
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGImageRef cgImage = [context createCGImage:outputImage
                                       fromRect:[outputImage extent]];
    
    UIImage *image = [UIImage imageWithCGImage:cgImage
                                         scale:1.
                                   orientation:UIImageOrientationUp];
    
    // Resize without interpolating
    UIImage *resized = [self resizeImage:image
                             withQuality:kCGInterpolationNone
                                    rate:10.0];//增加二维码清晰度
    
    CGImageRelease(cgImage);
    
    return resized;
}

+(UIImage *)resizeImage:(UIImage *)image
            withQuality:(CGInterpolationQuality)quality
                   rate:(CGFloat)rate{
    UIImage *resized = nil;
    CGFloat width = image.size.width * rate;
    CGFloat height = image.size.height * rate;
    
    UIGraphicsBeginImageContext(CGSizeMake(width, height));
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetInterpolationQuality(context, quality);
    [image drawInRect:CGRectMake(0, 0, width, height)];
    resized = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return resized;
}
@end
